package com.lz.sports.ui.score

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ConcatAdapter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.comm.BR
import com.lz.comm.image.GlideApp
import com.lz.helper.idcard.IDCardInfo
import com.lz.library.base.BaseActivity
import com.lz.library.extension.click
import com.lz.sports.R
import com.lz.sports.adapter.ScoreAdapter
import com.lz.sports.adapter.ScoreFootAdapter
import com.lz.sports.bean.CommonModel
import com.lz.sports.bean.Examinee
import com.lz.sports.core.ProjectManager
import com.lz.sports.databinding.ActScoreBinding
import com.lz.sports.ui.checkmodel.FingerDialog
import com.lz.sports.dialog.SignatureDialog
import com.lz.sports.ui.checkmodel.FaceIdentifyDialog
import com.lz.sports.ui.checkmodel.FeatureResult
import com.lz.sports.utils.UploadUtils
import com.lz.sports.vm.score.ScoreViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.File

//成绩确认的成绩界面
class ScoreActivity : BaseActivity<ScoreViewModel, ActScoreBinding>(),
    SignatureDialog.OnSignatureSureListener, FingerDialog.OnResultListener {

    private lateinit var adapter: ScoreAdapter

    private val footAdapter by lazy { ScoreFootAdapter() }

    //考生信息
    private lateinit var examinee: Examinee

    private lateinit var idCardInfo: IDCardInfo

    override fun getLayoutRes(): Int = R.layout.act_score

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        LiveEventBus.get("FaceIdentifyDialog", FeatureResult::class.java).observe(this) { featureResult ->
            binding.tvSure.visibility = View.INVISIBLE
            val sureTime = System.currentTimeMillis()
            footAdapter.setSureTime(sureTime)
            adapter.data.map { it.result }.takeIf { it.isNotEmpty() }
                ?.also { viewModel.sureScore(it, featureResult, sureTime) }
        }
    }

    @Suppress("DEPRECATION")
    override fun initData(savedState: Bundle?) {
        examinee = intent.getParcelableExtra("Examinee")!!
        idCardInfo = intent.getParcelableExtra("IDCardInfo")!!

        adapter = ScoreAdapter(examinee.resultWithInspect())
        binding.examinee = examinee
        binding.rvList.adapter = ConcatAdapter(adapter, footAdapter)
        GlideApp.with(this).load(examinee.examPhotoFile()).into(binding.ivHead)

        when (viewModel.sureModel) {
            CommonModel.NONE -> { //无须确认
                binding.tvSure.visibility = View.INVISIBLE
            }
            CommonModel.ID_CARD_EXAM, CommonModel.ID_CARD_ID, CommonModel.ID_CARD_FACE_EXAM, CommonModel.ID_CARD_AND -> { //人脸识别
                binding.tvSure.setText(R.string.sure_face)
            }
            CommonModel.ID_CARD_FINGER -> { //指纹确认方式
                binding.tvSure.setText(R.string.sure_finger)
            }
            CommonModel.ID_CARD_SIGNATURE -> { //手写签名
                binding.tvSure.setText(R.string.sure_sign)
            }
        }

        lifecycleScope.launch {
            binding.tvSure.click().collect {
                when (viewModel.sureModel) {
                    CommonModel.ID_CARD_EXAM, CommonModel.ID_CARD_ID, CommonModel.ID_CARD_FACE_EXAM, CommonModel.ID_CARD_AND -> { //人脸识别
                        FaceIdentifyDialog.show(
                            this@ScoreActivity, examinee, idCardInfo,
                            viewModel.sureModel, ProjectManager.personThreshold(), ProjectManager.faceThreshold()
                        )
                    }
                    CommonModel.ID_CARD_FINGER -> { //指纹确认方式
                        val dialog = FingerDialog.show(
                            this@ScoreActivity,
                            idCardInfo.finger,
                            ProjectManager.fingerprintThreshold()
                        )
                        dialog.onResultListener = this@ScoreActivity
                    }
                    CommonModel.ID_CARD_SIGNATURE -> { //手写签名
                        val path = File(UploadUtils.signaturePath(), "${examinee.examineesNo}.jpg").absolutePath
                        SignatureDialog.show(this@ScoreActivity, path)
                    }
                }
            }
        }
    }

    override fun onSure(dialog: SignatureDialog, status: Boolean, path: String) {
        dialog.dismiss()
        if (status) {
            //隐藏签名, 保存数据
            binding.tvSure.visibility = View.INVISIBLE
            val sureTime = System.currentTimeMillis()
            footAdapter.setSureTime(sureTime)
            adapter.data.map { it.result }.takeIf { it.isNotEmpty() }
                ?.also { viewModel.sureScore(it, idCardInfo, path, sureTime) }
        }
    }

    override fun onResult(threshold: Int) {
        binding.tvSure.visibility = View.INVISIBLE
        val sureTime = System.currentTimeMillis()
        footAdapter.setSureTime(sureTime)
        adapter.data.map { it.result }.takeIf { it.isNotEmpty() }
            ?.also { viewModel.sureScore(it, idCardInfo, threshold, sureTime) }
    }
}